package edu.unc.comp410.colemangeer.ui;

import javax.swing.*;
import javax.swing.event.*;

import org.apache.log4j.Logger;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.*;

import edu.unc.comp410.colemangeer.data.ICustomerGateway;
import edu.unc.comp410.colemangeer.demos.ShoppingCartDemo;
import edu.unc.comp410.colemangeer.model.*;
import edu.unc.comp410.colemangeer.system.UIController;

public class BrowseSpecificGUI2Model extends JFrame {
	
	//declarations
	//TODO: make this customer object come from the login use case
	Customer customer = new Customer( 1, "bob smith", "72 crystal oaks ct.", "Durham", "FL", "27707", 919, "357-4864" );
	private static Hashtable catItemHT; //initialize this to a very high prime number //hashtable has [productID, arrayOfDetails]
	private static Hashtable guiHT = new Hashtable();
	private Set<CatalogItem> catSet;
    public ShoppingCart cart;
	private static Set htKeySet;
	private static JButton enterButton = new JButton( "Enter" );
	private EnterHandler enterHandler;// = new EnterHandler();
	private UIController controller = new UIController();
	private static Logger log = Logger.getLogger(BrowseSpecificGUI2Model.class);
	
	/*
	DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object. 
	HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects. 
	DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects. 
	EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.
	*/
	
	//constructor
	public BrowseSpecificGUI2Model( Set<CatalogItem> theSet, ShoppingCart cart) {
		 super("Browse Products");
		 this.cart = cart;
		 enterHandler = new EnterHandler(this.cart);
		 setSize(300,700); //TODO: fix this size to be dynamic
		 setDefaultCloseOperation(HIDE_ON_CLOSE); //close the window, don't end the demo!
		 Container c = getContentPane();
		 c.setLayout(new GridLayout(0,2));
		 catSet = theSet;
		 
		 //translate theSet into a hashtable
		 catItemHT = setToHT(catSet);
		 
		 
		 //loop throught the product hashtable to create a row for each product
		 for (Object i : htKeySet) {
			 	Integer currentProdID = (Integer)i; //the current key in the Hashtable is the product ID
			 	
			 	//put JSpinner into gui hashtable - this was the only way to dynamically name the Spinners
			 	guiHT.put(currentProdID, new JSpinner());  //this is so we can access the spinner.getValue()
			 	
			 	//current-vars
			 	ArrayList currentArray = (ArrayList)catItemHT.get(currentProdID); //this array is the product details
			 	String currentLabel = currentArray.toString();						//this is the label to show on the swing pane
			 	JSpinner currentSpinner = (JSpinner)guiHT.get(currentProdID); 		//I drink coffee
			 	
			 	//place the stuff on the gui
			 	c.add( new JLabel( currentLabel ));
			 	c.add( currentSpinner );
		 }
		 
		 //buttons
		 c.add( new JLabel( "" ) );
		 enterButton.addActionListener(new EnterHandler(this.cart));
		 c.add(enterButton);
		
		 setVisible(true);
	}//end constructor
	
	//TODO: possibly refactor this to have values of Product objects as opposed to Arrays
	public Hashtable setToHT( Set<CatalogItem> theSet1 ){
		
		Hashtable result = new Hashtable();
		 
		for( CatalogItem i : theSet1 ){
			List pDetails = new ArrayList();
			pDetails.add(0, i.getId());
			pDetails.add(1, i.getName());
			pDetails.add(2, i.getPrice());
			pDetails.add( 3, i );  //this slot holds the actual object
			
			result.put(i.getId(),pDetails);
		}
		htKeySet = result.keySet();
		return result;
	}
	
	//TODO: make a getter method to deliver the result of this GUI
	
	
	private class EnterHandler implements ActionListener {
		
		ShoppingCart cart;
		
		public EnterHandler(ShoppingCart c){
			cart = c;
		}
		public void actionPerformed( ActionEvent e ) {
						
			//TODO: put try-catches in this gui code for bad entries or other errors
						
			//instantiate the cart
			//cart = new ShoppingCart(customer);
			
			//loop through the gui values and output them to screen
			for (Object i : htKeySet) {
			 	Integer currentCatID = (Integer)i;
			 	
			 	//current-vars  
			 	ArrayList currentArray = (ArrayList)catItemHT.get(currentCatID);
			 	CatalogItem currentCatalogItem = (CatalogItem)currentArray.get(3); //3 is the position of the catalogItem in the array
			 	JSpinner currentSpinner = (JSpinner)guiHT.get(currentCatID);
			 	Integer currentQuantity = (Integer)currentSpinner.getValue();
			 	String currentName = currentCatalogItem.getName();
			 	
			 	String currentLabel = currentArray.toString();
			 	String result = currentLabel + " " + currentQuantity;
			 	//System.out.println("currentName is: *" + currentName );
			 	//System.out.println("HOORAY!! result is: " + result );
			 	
			 	//dump items to the shopping cart now
			 	if( currentQuantity > 0 ){
			 		cart.addItem( currentCatalogItem, currentQuantity);
			 		//log.warn( currentQuantity + " " + currentName + "'s placed in cart");
			 	}
			 	
			}//end for-loop
		}//end actionperformed()
	} // End EnterHandler class.
	
	
} //end SpinnerTest class
